<?php
/**
 * Description
 * User: duanwenjie
 * Date: 2019/5/9
 * Time: 11:30 AM
 */

namespace app\index\controller;

use app\common\model\ToolModel;
use app\common\service\ExcelLibraryService;
use app\common\YujisoController;
use app\index\service\DrawplanService;
use app\index\service\DrawpodetailService;
use think\Exception;

class Drawplan extends YujisoController
{
    public $param;
    public function __construct()
    {
        parent::__construct();
        $temp = $this->request->param();
        if (isset($temp['s'])) unset($temp['s']);
        $this->param = $temp;
    }

    /**
     * 获取提货计划单列表
     * @author duanwenjie
     */
    public function getDrawplanList()
    {
        $param = $this->param;
        $result = DrawplanService::getDrawplanList($param);
        returnJson('1','查询成功',$result['list'],$result['total']);
    }


    /**
     * 启用/禁用
     * @author duanwenjie
     */
    public function disableOrEnable()
    {
        $param =$this->param;
        $rules = [
            'id' => 'require',
        ];
        $this->validateData($rules);
        ToolModel::updateData(['id' => $param['id']], ['status' => $param['status']],'purchase_plan');
        returnJson(1,'操作成功');
    }


    /**
     * 新增/编辑
     * @author duanwenjie
     */
    public function edit()
    {
        try{
            $param = $this->param;
            $id = "";
            if ($this->request->isPost()){
                /*编辑*/
                DrawplanService::edit($param);
                returnJson(1, "操作成功");
            }else{
                $result = DrawplanService::getDrawPlanDetail($param);
                returnJson(1, "",$result);
            }
        }catch (Exception $e){
            trace($e->getMessage(), 'error');
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 上传文件
     * @author duanwenjie
     */
    public function upload()
    {
        $param = array();
        $param['title'] = ['PO单号', '店铺', 'SKU', '理论需求数量', '本次提货数量', '最早收货日期', '最晚收货日期']; //必须校验头部
        $param['actionAsync'] = '\app\index\service\DrawplanService::uploadHandle'; //上传数据的数据回调到service层处理
        $res = ExcelLibraryService::read($param);
        exitJson($res);
    }

    /**
     * 导出数据
     * @author duanwenjie
     */
    public function export()
    {
        $param = $this->param;
        $res = DrawplanService::export($param);
        exitJson($res);
    }

    /**
     * 删除数据
     * @throws Exception
     * @author duanwenjie
     */
    public function del()
    {
        try{
            $param = $this->param;
            $id = "";
            if ($this->request->isPost()){
                /*删除*/
                if (isset($param['id']) && !empty($param['id'])){
                    $id = $param['id'];
                }
                if (!empty($id)){
                    ToolModel::deleteData(['id' => $id], 'draw_plan');
                    ToolModel::deleteData(['draw_plan_id' => $id], 'draw_plan_detail');
                    DrawplanService::sureDrawplan(array("is_need_update" => true));
                }else{
                    returnJson(500, "操作失败，ID不能为空");
                }
                returnJson(1, "操作成功");
            }
        }catch (Exception $e){
            trace($e->getMessage(), 'error');
            throw new Exception($e->getMessage());
        }
    }


    /**
     * 批量确定提货计划
     * @throws Exception
     * @author duanwenjie
     */
    public function sureDrawplan()
    {
        try{
            $param = $this->param;
            DrawplanService::sureDrawplan($param);
        }catch (Exception $e){
            trace($e->getMessage(), 'error');
            throw new Exception($e->getMessage());
        }
    }

}